import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { FontTabBar } from "../../../component/fontTabBar/FontTabBar";
import { Language } from "../../../../extend/language/Language";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { MuiEvent } from "../../../../com/mui/events/MuiEvent";
import { FirstMagicWeaponRankingCell } from "./FirstMagicWeaponRankingCell";
import { TilePanel } from "../equipment/TilePanel";
import { MagicWeaponCellRender } from "./MagicWeaponCellRender";
import { GTextFormat } from "../../../common/GTextFormat";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { PageSelecter } from "../common/pageSelect/PageSelecter";
import { FontUtil } from "../../../common/font/FontUtil";
import { SecTimerView } from "../common/SecTimerView";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { Cache } from "../../cache/Cache";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { ClockManager } from "../../manager/ClockManager";
import { ItemData } from "../../resource/info/ItemData";
import { BaseItemNormal } from "../common/item/BaseItemNormal";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { Global } from "../../../../com/gengine/global/Global";
import { SoundManager } from "../../../common/sound/SoundManager";
import { SoundTypeConst } from "../../../common/sound/SoundTypeConst";
import { TeamBuyRuleView } from "../TeamBuy/TeamBuyRuleView";
import { MagicWeaponRankingReward } from "./MagicWeaponRankingReward";
import { DataEvent } from "../../events/DataEvent";
import { GButton } from "../../../../com/mui/controls/GButton";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class MagicWeaponRankingWindow
    
    export  class MagicWeaponRankingWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._tabBar = new FontTabBar();
            this._tabArray = [];
            this.setSize(790, 530);
            titleHeight = 68;
            this.title = Language.getString(85069);
            return;
        }

        public updateRule(arg1: Boolean): void
        {
            if (!this._ruleView || this._ruleView.isHide) 
            {
                return;
            }
            if (arg1) 
            {
                this._ruleView.title = Language.getString(85081);
                this._ruleView.updateContent(Language.getString(85084));
            }
            else 
            {
                this._ruleView.title = Language.getString(85082);
                this._ruleView.updateContent(Language.getString(85083));
            }
            return;
        }

        public init(): void
        {
            ObjCreate.createBg(75, 45, 640, 40, this, ImagesConst.MagicWR_flowerbg);
            this._tabBar = new FontTabBar();
            this._tabBar.buttonStyleName = "DailyTabBarBtn";
            this._tabBar.x = 154;
            this._tabBar.y = 37;
            this._tabBar.buttonWidth = 196;
            this._tabBar.buttonHeight = 27;
            this._tabBar.disableHeight = 37;
            this._tabBar.disableWidth = 206;
            this._tabBar.horizontalGap = 30;
            this.addChild(this._tabBar);
            this._tabArray = [];
            this._tabArray.push({"label":"", "name":"crossServer", "img": ImagesConst.magicWR_crossServer});
            this._tabArray.push({"label":"", "name":"singleServer", "img": ImagesConst.magicWR_server});
            this._tabBar.addEventListener(MuiEvent.GTABBAR_SELECTED_CHANGE, this.onTabBarChangeHandler);
            ObjCreate.createBg(18, 81, 753, 99, this);
            ObjCreate.createBitmap(ImagesConst.magicWR_reward, 37, 92, this);
            this.createRewardList();
            this._firstItem = new FirstMagicWeaponRankingCell();
            this._firstItem.x = 266;
            this._firstItem.y = 78;
            addChild(this._firstItem);
            this._ruleBtn = ObjCreate.createGButton(Language.getString(85070), 574, 146, 67, 24, this);
            this._lookUpRewardBtn = ObjCreate.createGButton(Language.getString(85071), 652, 146, 67, 24, this);
            this._ruleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRuleBtnClickHandler);
            this._lookUpRewardBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onLookUpRewardBtnClickHandler);
            this._tenTilePanel = new TilePanel();
            this._tenTilePanel.x = 18;
            this._tenTilePanel.y = 182;
            this._tenTilePanel.setSize(753, 310);
            this._tenTilePanel.rowHeight = 99;
            this._tenTilePanel.columnWidth = 249;
            this._tenTilePanel.setStyle("cellRenderer", MagicWeaponCellRender);
            addChild(this._tenTilePanel);
            var loc1=new GTextFormat("", 12, 15788607, null, null, null, null, null, null, null, null, null, 3);
            this._tipTxt = ObjCreate.createTextField("", 529, 88, 232, 60, this, loc1, true, false);
            this._tipTxt.wordWrap = true;
            this._tipTxt.multiline = true;
            ObjCreate.createTextField(Language.getString(85072), 23, 496, 64, 17, this);
            this._rankTxt = ObjCreate.createTextField("", 85, 496, 64, 17, this, GlobalStyle.textFormat6);
            ObjCreate.createTextField(Language.getString(85074), 154, 496, 110, 17, this);
            this._scoreTxt = ObjCreate.createTextField("", 262, 496, 64, 17, this, GlobalStyle.textFormat5);
            this._pageSelecter = new PageSelecter();
            this._pageSelecter.mode = PageSelecter.CompleteMode;
            this._pageSelecter.x = 340;
            this._pageSelecter.y = 494;
            this._pageSelecter.currentPage = 1;
            this._pageSelecter.maxPage = 5;
            this._pageSelecter.addEventListener(flash.events.Event.CHANGE, this.onCurPageChangeHandler);
            this.addChild(this._pageSelecter);
            ObjCreate.createTextField(Language.getString(85075), 529, 496, 90, 17, this);
            ObjCreate.createTextField(Language.getString(85087), 636, 496, 148, 17, this, null, true, false);
            var loc2=new GTextFormat(FontUtil.EmbedNumberName, 18, GlobalStyle.color5Uint);
            loc2.letterSpacing = 2;
            this.curTimer = new SecTimerView();
            this.curTimer.embedFonts = true;
            this.curTimer.defaultTextFormat = loc2;
            this.curTimer.setParse("dd   hh   mm   ss");
            this.curTimer.filters = [FilterConst.glowFilter];
            this.curTimer.mouseEnabled = false;
            ObjCreate.setObjAttri(this.curTimer, 613, 494, 158, 20, this);
            this.isInit = true;
            return;
        }

        /* internal  */onCurPageChangeHandler(arg1: Object /* flash.events.Event */=null): void
        {
            this.updateData(this.curSelectIsCrossServer);
            return;
        }

        public get curSelectIsCrossServer(): Boolean
        {
            var loc1=this._tabBar.selectedItem["name"];
            return loc1 == "crossServer";
        }

        /* internal  */onTabBarChangeHandler(arg1: Object /* flash.events.Event */, arg2: Boolean=false): void
        {
            var loc3=null;
            var loc4=null;
            if (arg1 != null) 
            {
                this._pageSelecter.currentPage = 1;
            }
            var loc1=this.curSelectIsCrossServer;
            if (loc1) 
            {
                this.updateRewardItemData(this._crossServerRewardList);
                loc3 = Language.getString(85076);
                this._tipTxt.htmlText = loc3;
                if (arg2 || Cache.instance.magicWeaponRankingCache.crossServerData == null) 
                {
                    this.clearData();
                    Dispatcher.dispatchEvent(new Object /* flash.events.Event */(EventName.MagicWeaponRankingCrossServer));
                }
                else 
                {
                    this.updateData(true);
                }
            }
            else 
            {
                this.updateRewardItemData(this._singleRewardList);
                loc4 = Language.getString(85077);
                this._tipTxt.htmlText = loc4;
                if (arg2 || Cache.instance.magicWeaponRankingCache.curServerData == null) 
                {
                    this.clearData();
                    Dispatcher.dispatchEvent(new Object /* flash.events.Event */(EventName.MagicWeaponRankingCurrentServer));
                }
                else 
                {
                    this.updateData(false);
                }
            }
            var loc2=Cache.instance.magicWeaponRankingCache.getEndTime(loc1);
            this.curTimer.setLeftTime((loc2 - ClockManager.instance.nowDate.time) / 1000);
            this.updateRule(loc1);
            this.updateReward(loc1);
            return;
        }

        /* internal  */createRewardList(): void
        {
            var loc6=null;
            this._crossServerRewardList = new Vector.<ItemData>();
            this._singleRewardList = new Vector.<ItemData>();
            this._rewardItemsList = new Vector.<BaseItemNormal>();
            var loc1=GameDefConfig.instance.getMagicWeaponRankingReward(0);
            var loc2=GameDefConfig.instance.getMagicWeaponRankingReward(1);
            var loc3=loc1.length;
            var loc4=loc2.length;
            var loc5=0;
            while (loc5 < 5) 
            {
                (loc6 = new BaseItemNormal()).setSize(32, 32);
                loc6.isDragAble = false;
                loc6.isDropAble = false;
                ObjCreate.setObjAttri(loc6, 41 + loc5 * 44, 122, -1, -1, this);
                this._rewardItemsList.push(loc6);
                if (loc5 < loc3) 
                {
                    this._singleRewardList.push(new ItemData(int(loc1[loc5])));
                }
                if (loc5 < loc4) 
                {
                    this._crossServerRewardList.push(new ItemData(int(loc2[loc5])));
                }
                ++loc5;
            }
            return;
        }

        public updateRewardItemData(arg1: Vector.<ItemData>): void
        {
            var loc3=null;
            var loc1=arg1.length;
            var loc2=0;
            while (loc2 < 5) 
            {
                loc3 = this._rewardItemsList[loc2];
                if (loc2 < loc1) 
                {
                    loc3.itemData = arg1[loc2];
                    loc3.visible = true;
                }
                else 
                {
                    loc3.itemData = null;
                    loc3.visible = false;
                }
                ++loc2;
            }
            return;
        }

        public updateView(arg1: Boolean): void
        {
            var loc1=Cache.instance.magicWeaponRankingCache.canShowActive(false);
            var loc2=Cache.instance.magicWeaponRankingCache.canShowActive(true);
            if (loc1 && loc2) 
            {
                this._tabBar.dataProvider = this._tabArray;
                this._tabBar.x = 154;
            }
            else 
            {
                if (loc2) 
                {
                    this._tabBar.dataProvider = [this._tabArray[0]];
                }
                else 
                {
                    this._tabBar.dataProvider = [this._tabArray[1]];
                }
                this._tabBar.x = 270;
                this._tabBar.selectedIndex = 0;
            }
            this._tabBar.drawNow();
            this.onTabBarChangeHandler(null, arg1);
            return;
        }

        /* internal  */clearData(): void
        {
            this._tenTilePanel.dataProvider = new DataProvider([]);
            this._firstItem.data = null;
            return;
        }

        public updateData(arg1: Boolean): void
        {
            var loc2=0;
            var loc10=0;
            if (!this._tenTilePanel || !(arg1 == this.curSelectIsCrossServer)) 
            {
                return;
            }
            var loc1=Cache.instance.magicWeaponRankingCache;
            if (arg1) 
            {
                if (loc1.crossServerData == null) 
                {
                    return;
                }
                loc2 = Math.ceil(loc1.crossServerData.length / 10);
            }
            else 
            {
                if (loc1.curServerData == null) 
                {
                    return;
                }
                loc2 = Math.ceil(loc1.curServerData.length / 10);
            }
            if (this._pageSelecter.maxPage != loc2) 
            {
                this._pageSelecter.maxPage = loc2;
            }
            var loc3=this._pageSelecter.currentPage;
            var loc4;
            if (!(loc4 = loc1.getPage(arg1, loc3, 10)) || loc4.length <= 0) 
            {
                this.clearData();
                return;
            }
            this._firstItem.data = loc4[0];
            this._tenTilePanel.dataProvider = new DataProvider(loc4.slice(1));
            var loc5=loc1.curServerMagicWeaponType - 1401;
            var loc6=Cache.instance.pack.backPackCache.getMagicWeaponItemMaxScore(loc5);
            var loc7=Cache.instance.pack.rolePackCache.getMagicWeaponItemMaxScore(loc5);
            var loc8=Cache.instance.pack.wareHousePackCache.getMagicWeaponItemMaxScore(loc5);
            var loc9;
            if ((loc9 = Math.max(loc6, loc7, loc8)) != -1) 
            {
                this._scoreTxt.text = String(loc9);
            }
            else 
            {
                this._scoreTxt.text = Language.getString(85078);
            }
            if (arg1) 
            {
                loc10 = loc1.myCrossRank;
            }
            else 
            {
                loc10 = loc1.myRank;
            }
            if (loc10 != -1) 
            {
                this._rankTxt.text = String(loc10);
            }
            else 
            {
                this._rankTxt.text = Language.getString(85073);
            }
            return;
        }

        public /* override */ hide(): void
        {
            Global.stage.focus = Global.stage;
            SoundManager.instance.soundPlay(SoundTypeConst.UIclose);
            if (this._ruleView && this._ruleView.isHide == false) 
            {
                this._ruleView.hide();
            }
            if (this._rewardWindow && this._rewardWindow.isHide == false) 
            {
                this._rewardWindow.hide();
            }
            this.curTimer.stop();
            super.hide();
            return;
        }

        /* internal  */onRuleBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._rewardWindow && this._rewardWindow.isHide == false) 
            {
                this._rewardWindow.hide();
            }
            if (!this._ruleView) 
            {
                this._ruleView = new TeamBuyRuleView(this);
            }
            if (this._ruleView.isHide) 
            {
                this._ruleView.show();
                this.updateRule(this.curSelectIsCrossServer);
            }
            else 
            {
                this._ruleView.hide();
            }
            return;
        }

        /* internal  */onLookUpRewardBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=this.curSelectIsCrossServer;
            if (this._ruleView && this._ruleView.isHide == false) 
            {
                this._ruleView.hide();
            }
            if (this._rewardWindow == null) 
            {
                this._rewardWindow = new MagicWeaponRankingReward(this);
            }
            if (this._rewardWindow.isHide) 
            {
                this._rewardWindow.show();
                this.updateReward(loc1);
            }
            else 
            {
                this._rewardWindow.hide();
            }
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            if (this.isHide) 
            {
                this.updateView(true);
            }
            super.show(arg1, arg2);
            return;
        }

        public updateReward(arg1: Boolean): void
        {
            var loc1=null;
            var loc2=null;
            if (!this._rewardWindow || this._rewardWindow.isHide) 
            {
                return;
            }
            if (arg1) 
            {
                this._rewardWindow.title = Language.getString(85079);
                loc2 = Cache.instance.magicWeaponRankingCache.crossServerRewardMap;
            }
            else 
            {
                this._rewardWindow.title = Language.getString(85080);
                loc2 = Cache.instance.magicWeaponRankingCache.currentServerRewardMap;
            }
            if (loc2 != null) 
            {
                this._rewardWindow.updateRewardList(loc2);
            }
            else if (arg1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRankingRewardGet, 1));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponRankingRewardGet, 0));
            }
            return;
        }

        /* internal */ /* var */_tenTilePanel: TilePanel;

        /* internal */ /* var */_pageSelecter: PageSelecter;

        public /* var */curTimer: SecTimerView;

        /* internal */ /* var */_firstItem: FirstMagicWeaponRankingCell;

        public /* var */isInit: Boolean;

        /* internal */ /* var */_ruleBtn: GButton;

        /* internal */ /* var */_lookUpRewardBtn: GButton;

        /* internal */ /* var */_rewardWindow: MagicWeaponRankingReward;

        /* internal */ /* var */_rankTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_scoreTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_rewardItemsList: Vector.<BaseItemNormal>;

        /* internal */ /* var */_crossServerRewardList: Vector.<ItemData>;

        /* internal */ /* var */_singleRewardList: Vector.<ItemData>;

        /* internal */ /* var */_ruleView: TeamBuyRuleView;

        /* internal */ /* var */_tabBar: FontTabBar;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_tabArray: Array<any>;
    }
